package pos.data;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.csvreader.CsvWriter;

import pos.domain.CreditCard;

public class CSVCreditCardDAO implements ICreditCardDAO {

	private CsvWriter openFile() {

		CsvWriter csvOutput = null;
		String outputFile = "data/creditcard.csv";

		// before we open the file check to see if it already exists
		boolean alreadyExists = new File(outputFile).exists();

		try {
			// use FileWriter constructor that specifies open for appending
			csvOutput = new CsvWriter(new FileWriter(outputFile, true), ',');

			// if the file didn't already exist then we need to write out the
			// header line
			if (!alreadyExists) {
				csvOutput.write("OID");
				csvOutput.write("holder");
				csvOutput.write("number");
				csvOutput.write("month");
				csvOutput.write("year");
				csvOutput.endRecord();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

		return csvOutput;

	}

	private void closeFile(CsvWriter writer) {
		writer.close();

	}
	
    public void delete(String creditCardOID) {
    }

    public void insert(String creditCardOID, CreditCard c) {
    	
    	CsvWriter writer = this.openFile();
      	
    	try {
    		writer.write(creditCardOID);
			writer.write(c.getHolder());
			writer.write(c.getNumber());
			writer.write(String.valueOf(c.getMonth()));
			writer.write(String.valueOf(c.getYear()));
			writer.endRecord();
    	} catch (IOException e) {
			e.printStackTrace();
		}
        
    	closeFile(writer);    	
    	
    	
    }

    public CreditCard select( String creditCardOID) {
		return null;
    }

}

